package com.chiancloud.shield.common.params;

import com.chiancloud.shield.common.ShieldXmlConfKeys;
import com.chiancloud.shield.core.exception.BadCommandArgumentsException;
import com.chiancloud.shield.core.exception.ShieldException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.yarn.conf.YarnConfiguration;


import java.util.Collection;


public class ClientArgs extends CommonArgs {


  private AbstractClusterBuildingActionArgs buildingActionArgs;
  private final ActionAMSuicideArgs actionAMSuicideArgs = new ActionAMSuicideArgs();
  private final ActionBuildArgs actionBuildArgs = new ActionBuildArgs();
  private final ActionCreateArgs actionCreateArgs = new ActionCreateArgs();
  private final ActionDestroyArgs actionDestroyArgs = new ActionDestroyArgs();
  private final ActionExistsArgs actionExistsArgs = new ActionExistsArgs();
  private final ActionFlexArgs actionFlexArgs = new ActionFlexArgs();
  private final ActionFreezeArgs actionFreezeArgs = new ActionFreezeArgs();
  private final ActionGetConfArgs actionGetConfArgs = new ActionGetConfArgs();
  private final ActionKillContainerArgs actionKillContainerArgs =
    new ActionKillContainerArgs();
  private final ActionListArgs actionListArgs = new ActionListArgs();
  private final ActionRegistryArgs actionRegistryArgs = new ActionRegistryArgs();
  private final ActionStatusArgs actionStatusArgs = new ActionStatusArgs();
  private final ActionThawArgs actionThawArgs = new ActionThawArgs();
  private final ActionVersionArgs actionVersionArgs = new ActionVersionArgs();
  private final ActionHelpArgs actionHelpArgs = new ActionHelpArgs();


  public ClientArgs(String[] args) {
    super(args);
  }

  public ClientArgs(Collection args) {
    super(args);
  }

  @Override
  protected void addActionArguments() {

    addActions(
      actionAMSuicideArgs,
      actionBuildArgs,
      actionCreateArgs,
      actionDestroyArgs,
      actionExistsArgs,
      actionFlexArgs,
      actionFreezeArgs,
      actionGetConfArgs,
      actionKillContainerArgs,
      actionListArgs,
      actionRegistryArgs,
      actionStatusArgs,
      actionThawArgs,
      actionHelpArgs,
      actionVersionArgs
              );
  }

  @Override
  public void applyDefinitions(Configuration conf) throws
          BadCommandArgumentsException {
    super.applyDefinitions(conf);
    //RM
    if (getManager() != null) {
      log.debug("Setting RM to {}", getManager());
      conf.set(YarnConfiguration.RM_ADDRESS, getManager());
    }
    if ( getBasePath() != null ) {
      log.debug("Setting basePath to {}", getBasePath());
      conf.set(ShieldXmlConfKeys.KEY_SLIDER_BASE_PATH, getBasePath().toString());
    }
  }

  public AbstractClusterBuildingActionArgs getBuildingActionArgs() {
    return buildingActionArgs;
  }

  public ActionAMSuicideArgs getActionAMSuicideArgs() {
    return actionAMSuicideArgs;
  }

  public ActionBuildArgs getActionBuildArgs() {
    return actionBuildArgs;
  }

  public ActionCreateArgs getActionCreateArgs() {
    return actionCreateArgs;
  }

  public ActionDestroyArgs getActionDestroyArgs() {
    return actionDestroyArgs;
  }

  public ActionExistsArgs getActionExistsArgs() {
    return actionExistsArgs;
  }

  public ActionFlexArgs getActionFlexArgs() {
    return actionFlexArgs;
  }

  public ActionFreezeArgs getActionFreezeArgs() {
    return actionFreezeArgs;
  }

  public ActionGetConfArgs getActionGetConfArgs() {
    return actionGetConfArgs;
  }

  public ActionKillContainerArgs getActionKillContainerArgs() {
    return actionKillContainerArgs;
  }

  public ActionListArgs getActionListArgs() {
    return actionListArgs;
  }

  public ActionRegistryArgs getActionRegistryArgs() {
    return actionRegistryArgs;
  }

  public ActionStatusArgs getActionStatusArgs() {
    return actionStatusArgs;
  }

  public ActionThawArgs getActionThawArgs() {
    return actionThawArgs;
  }


  @Override
  public void applyAction() throws ShieldException {
    String action = getAction();
    if (SliderActions.ACTION_BUILD.equals(action)) {
      bindCoreAction(actionBuildArgs);
      //its a builder, so set those actions too
      buildingActionArgs = actionBuildArgs;
    } else if (SliderActions.ACTION_CREATE.equals(action)) {
      bindCoreAction(actionCreateArgs);
      //its a builder, so set those actions too
      buildingActionArgs = actionCreateArgs;

    } else if (SliderActions.ACTION_FREEZE.equals(action)) {
      bindCoreAction(actionFreezeArgs);

    } else if (SliderActions.ACTION_THAW.equals(action)) {
      bindCoreAction(actionThawArgs);

    } else if (SliderActions.ACTION_AM_SUICIDE.equals(action)) {
      bindCoreAction(actionAMSuicideArgs);

    } else if (SliderActions.ACTION_DESTROY.equals(action)) {
      bindCoreAction(actionDestroyArgs);

    } else if (SliderActions.ACTION_EXISTS.equals(action)) {
      bindCoreAction(actionExistsArgs);

    } else if (SliderActions.ACTION_FLEX.equals(action)) {
      bindCoreAction(actionFlexArgs);

    } else if (SliderActions.ACTION_GETCONF.equals(action)) {
      bindCoreAction(actionGetConfArgs);

    } else if (SliderActions.ACTION_HELP.equals(action) ||
               SliderActions.ACTION_USAGE.equals(action)) {
      bindCoreAction(actionHelpArgs);

    } else if (SliderActions.ACTION_KILL_CONTAINER.equals(action)) {
      bindCoreAction(actionKillContainerArgs);

    } else if (SliderActions.ACTION_LIST.equals(action)) {
      bindCoreAction(actionListArgs);

    } else if (SliderActions.ACTION_REGISTRY.equals(action)) {
      bindCoreAction(actionRegistryArgs);

    } else if (SliderActions.ACTION_STATUS.equals(action)) {
      bindCoreAction(actionStatusArgs);

    } else if (SliderActions.ACTION_VERSION.equals(action)) {
      bindCoreAction(actionVersionArgs);

    } else if (action == null || action.isEmpty()) {
      throw new BadCommandArgumentsException(ErrorStrings.ERROR_NO_ACTION);

    } else {
      throw new BadCommandArgumentsException(ErrorStrings.ERROR_UNKNOWN_ACTION
                                             + " " + action);
    }
  }
}
